package com.example.file.test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CustomWindow extends JFrame {

    private JPanel middlePanel;
    private JButton button1;
    private JButton button2;
    private JPanel tablePanel; // 用于存放表格和翻页按钮的面板

    public CustomWindow() {
        // 初始化窗口
        setTitle("Custom Window");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 初始化中间面板
        middlePanel = new JPanel();
        add(middlePanel, BorderLayout.CENTER);

        // 初始化按钮并添加事件监听器
        button1 = new JButton("Input Fields");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showInputFields();
            }
        });

        button2 = new JButton("Table with Pagination");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showTableWithPagination();
            }
        });

        // 添加按钮到窗口的左侧
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
        buttonPanel.add(button1);
        buttonPanel.add(button2);
        add(buttonPanel, BorderLayout.WEST);

        // 初始显示输入框
        showInputFields();

        // 设置窗口可见
        setVisible(true);
    }

    private void showInputFields() {
        // 清空中间面板并添加上下两个输入框
        middlePanel.removeAll();
        JTextField upperTextField = new JTextField(20);
        JTextField lowerTextField = new JTextField(20);
        middlePanel.add(upperTextField);
        middlePanel.add(lowerTextField);
        middlePanel.add(Box.createVerticalStrut(10));
        middlePanel.revalidate();
        middlePanel.repaint();
    }

    private void showTableWithPagination() {
        // 清空中间面板并添加表格和翻页按钮
        middlePanel.removeAll();

        // 假设的表格数据
        String[][] data = {
                {"Row1 Col1", "Row1 Col2", "Row1 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                {"Row2 Col1", "Row2 Col2", "Row2 Col3"},
                // ... 更多行数据 ...
        };
        String[] columnNames = {"Column 1", "Column 2", "Column 3"};
        JTable table = new JTable(data, columnNames);

        // 创建翻页按钮
        JPanel paginationPanel = new JPanel();
        JButton prevButton = new JButton("Prev");
        JButton nextButton = new JButton("Next");

        // 这里应该添加翻页逻辑，这里只是示例按钮
        prevButton.setEnabled(false); // 初始时假设没有上一页
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 这里应该实现翻页的逻辑，这里只是简单地禁用下一个按钮作为示例
                nextButton.setEnabled(false);
            }
        });

        paginationPanel.add(prevButton);
paginationPanel.add(nextButton);
        // 将表格和翻页按钮添加到中间面板
        JScrollPane scrollPane = new JScrollPane(table);
        middlePanel.add(scrollPane);
        middlePanel.add(paginationPanel);
        middlePanel.revalidate();
        middlePanel.repaint();
    }

    public static void main(String[] args) {
        // 创建并显示窗口
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CustomWindow();
            }
        });
    }
}